<?php

//session_start();
header("Content-type:text/html;charset=utf-8");
//include ('peizhi.php');
//define your token
define("TOKEN", "zhuoling");
date_default_timezone_set("PRC");

$wechatObj = new wechatCallbackapiTest();

if (!isset($_GET['echostr'])) {  
    $wechatObj->responseMsg();  
}else{  
    $wechatObj->valid();  
}  
  

//$wechatObj->responseMsg();


class wechatCallbackapiTest
{
	public function valid()
    {
        $echoStr = $_GET["echostr"];
        if($this->checkSignature()){
        	echo $echoStr;
        	exit;
        }
    }
    public function responseMsg()
    {
        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
        if (!empty($postStr)){
            $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
            $fromUsername = $postObj->FromUserName;
            $toUsername = $postObj->ToUserName;
            $keyword = trim($postObj->Content);
            $type = $postObj->MsgType;
            $customevent = $postObj->Event;
			$imgurl=$postObj->MediaId;
			$EventKey=$postObj->EventKey;
			$event=$postObj->Event;
			//$EventKey=$postObj->EventKey;
			
			
			
				//echo "";
				
				
               mysql_connect("127.0.0.1", "root","db692a4f05") ;
				   
				mysql_select_db("zhetu");
				   
				mysql_query("set names utf8");
			
			  /*
					$sql="insert into test(type,picurl,msgid,event)values('".$type."','".$imgurl."','".$postObj->MsgId."','".$event."')";
				
				mysql_query($sql);*/
				
				
				 //找出最低
				
				$sql="select * from system where id=1";
				$zuidi=mysql_query($sql);
			    $z=mysql_fetch_assoc($zuidi);
				
				   //查找出本站网址
			  $sql="select * from system where id=1";
			  $url=mysql_query($sql);
			  $row=  mysql_fetch_assoc($url);
				
				//这里获取token
			    $sql="select * from token where id=1";
				$token=mysql_query($sql);
				$token=mysql_fetch_assoc($token);
			    $time=time();
				if($time>$token['time']){
				
				$sql="select * from wx where id=1";
				$wx=mysql_query($sql);
				$wx=mysql_fetch_assoc($wx);
				
				
				$re=file_get_contents("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$wx['appid']."&secret=".$wx['appsecret']);
				$re=json_decode($re,true);
				$timenow=$time+3600;
				//$sql="insert into token(token,time)values('".$re['access_token']."','".$timenow."')";
				$sql="update token set token='".$re['access_token']."',time='".$timenow."' where id='1'";
				mysql_query($sql);
				 }  
				 
                $sql="select * from token where id=1";
				$token=mysql_query($sql);
				$token=mysql_fetch_assoc($token);	
				
				
				
				 //如果是新用户存入数据库
			   $sql="select * from user where openid='".$fromUsername."'";
			   $u=mysql_query($sql);
			   $u=mysql_fetch_array($u);
			   if($u==false){
			   
			$url1 = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$token['token']."&openid=".$fromUsername."&lang=zh_CN ";
				  $json1 = file_get_contents($url1);//获取微信用户基本信息
				  $arr1 = json_decode($json1,true);
			   
			      $sql="insert into user(openid,money,nicheng,touxiang)values('".$fromUsername."','0','".$arr1['nickname']."','".$arr1['headimgurl']."')";
				  mysql_query($sql);
			   
			   }
			
			
			
			  //这里来处理视频
			  
			  if($imgurl!="" and $type=="video"){
			  
			  
			   
			   
			  
			   //告诉用户正在处理
			   
			   $sql="select * from paichong where mid='".$postObj->MsgId."'";
			   $remid=mysql_query($sql);
			   $ishaveid = mysql_fetch_array($remid, MYSQL_ASSOC);  
			   if($ishaveid==false){
			   
			    $sql="insert into paichong(mid,time)values('".$postObj->MsgId."','".time()."')";
				mysql_query($sql);
			   
			   
			    $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$token['token'];
                $data = '{
                    "touser": "'.$fromUsername.'", 
                    "msgtype": "text", 
                    "text": {
                        "content": "请稍后，我们正在努力处理中！"
                    }
                }';
                $this->https_request($url,$data);
				
			    $url="http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=".$token['token']."&media_id=".$imgurl;
				
				
				//$token="0mRE1dEp3Z3ImVQyr97frV6tlzpvC7d4JrjkqnQ9NHDB9WW10FZ7lkCDMthplBl7D2Q7I53RmnkDmfxShu8lOQO9LwnTIJbYT9nFE85cOXT_MU1tPLpPDpB7cdI1yjnIEWBbAHANFK";
			
				
				
				
				
			
				
				
				$str="1234567890abcdefghigklmnopqrstuvwxyz";
				$str=str_shuffle($str);
				$str=substr($str,0,8);
				$str=time().$str;
				
				
				
				$xinzhuanhua=$str.".mp4";
				
				$url1 = "https://api.weixin.qq.com/cgi-bin/media/get?access_token=".$token['token']."&media_id=".$imgurl;
				$arr = $this->downloadWeixinFile($url1);
				$this->saveWeixinFile("./images/".$xinzhuanhua,$arr['body']);
				
				
				
				
				 $url="http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=".$token['token']."&media_id=".$postObj->ThumbMediaId;
			   
			   $smallimg=$this->xiazai($url);
			   
			   
			    include ("./mohu.php");
			
			
               $test=new test();

             //  $mohu=$test->gaussian_blur('./images/'.$smallimg,'','b-'.$smallimg,$row['mohudu']);
			   
			    $mohu=$test->gaussian_blur('./images/'.$smallimg,'','b-'.$smallimg,3);
				
			    $this->changesize('./images/b-'.$smallimg);
			    if($mohu){
				
				     //存入订单表
			    $mohuimg='b-'.$smallimg;
			    $sql="insert into orderinfo(qingxi,mohu,time,openid,suijistart,isvideo,video,suijiend,gudingmoney)values('".$smallimg."','".$mohuimg."','".time()."','".$fromUsername."','".$z['zuidi']."','1','".$xinzhuanhua."','".$z['zuigao']."','3')";
			    mysql_query($sql);
				$orderid = mysql_insert_id();
				
				
				  require_once './phpqrcode/phpqrcode.php';
                        
                 $data = $row['url']."?id=".$orderid;

		      // 纠错级别：L、M、Q、H

		         $level = 'Q';

		      // 点的大小：1到10,用于手机端4就可以了

		         $size = 6;

		     // 下面注释了把二维码图片保存到本地的代码,如果要保存图片,用$fileName替换第二个参数false

		         $path = "./images/";

		     // 生成的文件名

		         $fileName = $path.'e-'.$smallimg.'.png';

		         QRcode::png($data, $fileName, $level, $size);
				 
				 
				  //图片上写上字
			  
			    include ('./xinshuiyin.php');
			   
			   $markimg = new ImageWatermark('./images/b-'.$smallimg);

				$markimg->markPosType=7;
				
				$markimg->fontFile='wr.TTF';
				
				$markimg->color='#ffffff';
				
				$markimg->fontSize=22;
				
				$markimg->appendTextMark('长按二维码看视频',255,50);
				
				
				$markimg->write("./images/m-".$smallimg);
				 
				 //图片上打上二维码
				
				include ('./tupian.php');
				imageWaterMark1("./images/m-".$smallimg, 5, "play.png");
			    imageWaterMark1("./images/m-".$smallimg, 7, "./images/e-".$smallimg.".png");
			   
			   
			    //上传素材
				
				 $url="http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=".$token['token']."&type=image";
				$file = realpath("./images/m-".$smallimg); //要上传的文件
				$fields['media'] = '@'.$file;
				$ch = curl_init($url) ;
				curl_setopt($ch, CURLOPT_POST, 1);
				curl_setopt($ch, CURLOPT_POSTFIELDS,$fields);
				curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
				$myimg = curl_exec($ch);
				if (curl_errno($ch)) {
				 return curl_error($ch);
				}
				curl_close($ch);				
				$myimg=json_decode($myimg,true);
				
				
				 //客户消息回复图片
				$url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$token['token'];
                $data = '{
                    "touser": "'.$fromUsername.'", 
                    "msgtype": "image", 
                    "image": {
                       "media_id":"'.$myimg['media_id'].'"
                    }
                }';
                $this->https_request($url,$data);
			   
			   
				//}
			   
			    echo "";
				}
				
				}else{
				
				
				  echo "";
				}
				
				
				
			  
			  }
			  
			
			
			
			    
			   //这里来处小理视频
			  
			  if($imgurl!="" and $type=="shortvideo"){
			  
			   
			  
			   //告诉用户正在处理
			   
			   $sql="select * from paichong where mid='".$postObj->MsgId."'";
			   $remid=mysql_query($sql);
			   $ishave = mysql_fetch_array($remid, MYSQL_ASSOC);  
			   if($ishave==false){
			   
			    $sql="insert into paichong(mid,time)values('".$postObj->MsgId."','".time()."')";
				mysql_query($sql);
			   
			   
			    $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$token['token'];
                $data = '{
                    "touser": "'.$fromUsername.'", 
                    "msgtype": "text", 
                    "text": {
                        "content": "请稍后，我们正在努力处理中！"
                    }
                }';
                $this->https_request($url,$data);
				
			    $url="http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=".$token['token']."&media_id=".$imgurl;
				
				$newimg=$this->xiazai($url);
				
				
					
				$zhuanhua='./images/'.$newimg;
				
				$str="1234567890abcdefghigklmnopqrstuvwxyz";
				$str=str_shuffle($str);
				$str=substr($str,0,8);
				$str=time().$str;
				
				
				
				$xinzhuanhua=$str.".mp4";
				
			  
				
				$url1 = "https://api.weixin.qq.com/cgi-bin/media/get?access_token=".$token['token']."&media_id=".$imgurl;
				$arr = $this->downloadWeixinFile($url1);
				$this->saveWeixinFile("./images/".$xinzhuanhua,$arr['body']);
				 $url="http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=".$token['token']."&media_id=".$postObj->ThumbMediaId;
			   
			   $smallimg=$this->xiazai($url);
			   
			   
			    include ("./mohu.php");
			
			
               $test=new test();

             //  $mohu=$test->gaussian_blur('./images/'.$smallimg,'','b-'.$smallimg,$row['mohudu']);
			   
			    $mohu=$test->gaussian_blur('./images/'.$smallimg,'','b-'.$smallimg,3);
				
			    $this->changesize('./images/b-'.$smallimg);
			    if($mohu){
				
				     //存入订单表
			    $mohuimg='b-'.$smallimg;
			      $sql="insert into orderinfo(qingxi,mohu,time,openid,suijistart,isvideo,video,suijiend,gudingmoney)values('".$smallimg."','".$mohuimg."','".time()."','".$fromUsername."','".$z['zuidi']."','1','".$xinzhuanhua."','".$z['zuigao']."','3')";
			    mysql_query($sql);
				$orderid = mysql_insert_id();
				
				
				  require_once './phpqrcode/phpqrcode.php';
                        
                 $data = $row['url']."?id=".$orderid;

		      // 纠错级别：L、M、Q、H

		         $level = 'Q';

		      // 点的大小：1到10,用于手机端4就可以了

		         $size = 6;

		     // 下面注释了把二维码图片保存到本地的代码,如果要保存图片,用$fileName替换第二个参数false

		         $path = "./images/";

		     // 生成的文件名

		         $fileName = $path.'e-'.$smallimg.'.png';

		         QRcode::png($data, $fileName, $level, $size);
				 
				 
				  //图片上写上字
			  
			    include ('./xinshuiyin.php');
			   
			   $markimg = new ImageWatermark('./images/b-'.$smallimg);

				$markimg->markPosType=7;
				
				$markimg->fontFile='wr.TTF';
				
				$markimg->color='#ffffff';
				
				$markimg->fontSize=22;
				
				$markimg->appendTextMark('长按二维码看视频',255,50);
				
				
				$markimg->write("./images/m-".$smallimg);
				 
				 //图片上打上二维码
				
				include ('./tupian.php');
				imageWaterMark1("./images/m-".$smallimg, 5, "play.png");
			    imageWaterMark1("./images/m-".$smallimg, 7, "./images/e-".$smallimg.".png");
			   
			   
			    //上传素材
				
				 $url="http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=".$token['token']."&type=image";
				$file = realpath("./images/m-".$smallimg); //要上传的文件
				$fields['media'] = '@'.$file;
				$ch = curl_init($url) ;
				curl_setopt($ch, CURLOPT_POST, 1);
				curl_setopt($ch, CURLOPT_POSTFIELDS,$fields);
				curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
				$myimg = curl_exec($ch);
				if (curl_errno($ch)) {
				 return curl_error($ch);
				}
				curl_close($ch);				
				$myimg=json_decode($myimg,true);
				
				
				 //客户消息回复图片
				$url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$token['token'];
                $data = '{
                    "touser": "'.$fromUsername.'", 
                    "msgtype": "image", 
                    "image": {
                       "media_id":"'.$myimg['media_id'].'"
                    }
                }';
                $this->https_request($url,$data);
			   
			   
				}
			   
			    echo "";
			//	}
				
				}else{
				
				
				  echo "";
				}
				
				
				
			  
			  }
			
			
			   
			   
			   //这里处理图片 
		
			   if($imgurl!='' and $type=="image"){
			   
			   
			   
			   $sql="select * from paichong where mid='".$postObj->MsgId."'";
			   $remid=mysql_query($sql);
			   $ishave = mysql_fetch_array($remid, MYSQL_ASSOC);  
			   if($ishave==false){
			   
			    $sql="insert into paichong(mid,time)values('".$postObj->MsgId."','".time()."')";
				mysql_query($sql);
			   
			   //告诉用户正在处理
			   
			    $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$token['token'];
                $data = '{
                    "touser": "'.$fromUsername.'", 
                    "msgtype": "text", 
                    "text": {
                        "content": "请稍后，我们正在努力处理中！"
                    }
                }';
                $this->https_request($url,$data);
				
			  
			    //这里处理下载用户上传的图片				
				
				$url="http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=".$token['token']."&media_id=".$imgurl;
				
				$newimg=$this->xiazai($url);
			
			
			if($row['isjianhuang']==0){
			
			 $picinfo = 0;
			
			}else{	
				
			$shibie = $this->pic64($newimg);
           //验证图片级别
		    $picinfo = $this->picyanzheng($shibie,$row['key']);	
			
			}
			
			$sql="insert into test(mid)values('".$picinfo."')";
			mysql_query($sql);
				
			//识别结束	来做判断
			if($picinfo>$row['sexnum']){
			
			  $jinggao=$row['jinggao'];
			
			//  unlink("./images/".$newimg);
			    $xmlTpl = "<xml>
 <ToUserName><![CDATA[%s]]></ToUserName>
 <FromUserName><![CDATA[%s]]></FromUserName>
 <CreateTime>%s</CreateTime>
 <MsgType><![CDATA[text]]></MsgType>
 <Content><![CDATA[%s]]></Content>
 
 </xml>";
             $result = sprintf($xmlTpl, $postObj->FromUserName, $postObj->ToUserName,time(),$jinggao);
			 echo $result;
			    
			
			}else{
				
			  
			    //$pic     = "./images/".$newimg;
             	//$picblur = "./images/b-".$newimg;
			
			    include ("./mohu.php");
			
			
               $test=new test();

               $mohu=$test->gaussian_blur('./images/'.$newimg,'','b-'.$newimg,$row['mohudu']);
			   $this->changesize('./images/b-'.$newimg);
			   if($mohu){
			   
			   
				
			    //存入订单表
			    $mohuimg='b-'.$newimg;
			    $sql="insert into orderinfo(qingxi,mohu,time,openid,suijistart,suijiend,gudingmoney)values('".$newimg."','".$mohuimg."','".time()."','".$fromUsername."','".$z['zuidi']."','".$z['zuigao']."','3')";
			    mysql_query($sql);
				$orderid = mysql_insert_id();
				
				
				 require_once './phpqrcode/phpqrcode.php';
                        
                 $data = $row['url']."?id=".$orderid;

		      // 纠错级别：L、M、Q、H

		         $level = 'H';

		      // 点的大小：1到10,用于手机端4就可以了

		         $size = 6;

		     // 下面注释了把二维码图片保存到本地的代码,如果要保存图片,用$fileName替换第二个参数false

		         $path = "./images/";

		     
		     // 生成的文件名

		      $fileName = $path.'e-'.$newimg.'.png';

		      QRcode::png($data, $fileName, $level, $size);
			  
			  
			  
			    //图片上写上字
			  
			    include ('./xinshuiyin.php');
			   
			   $markimg = new ImageWatermark('./images/b-'.$newimg);

				$markimg->markPosType=7;
				
				$markimg->fontFile='wr.TTF';
				
				$markimg->color='#ffffff';
				
				$markimg->fontSize=22;
				
				$markimg->appendTextMark('长按二维码看原图',255,50);
				
				
				$markimg->write("./images/m-".$newimg);
				
				
				//图片上打上二维码
				
				include ('./tupian.php');
			    imageWaterMark1("./images/m-".$newimg, 7, "./images/e-".$newimg.".png");
				
				//上传素材
				
				 $url="http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=".$token['token']."&type=image";
				$file = realpath("./images/m-".$newimg); //要上传的文件
				$fields['media'] = '@'.$file;
				$ch = curl_init($url) ;
				curl_setopt($ch, CURLOPT_POST, 1);
				curl_setopt($ch, CURLOPT_POSTFIELDS,$fields);
				curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
				$myimg = curl_exec($ch);
				if (curl_errno($ch)) {
				 return curl_error($ch);
				}
				curl_close($ch);				
				$myimg=json_decode($myimg,true);
				
				
				 //客户消息回复图片
				$url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$token['token'];
                $data = '{
                    "touser": "'.$fromUsername.'", 
                    "msgtype": "image", 
                    "image": {
                       "media_id":"'.$myimg['media_id'].'"
                    }
                }';
                $this->https_request($url,$data);
			   
			   
			   
			   }
			   
			     echo "";
				 }

				 }else{
				 
				 echo "";
				 
				 }
			   
              }
			
			
			
			
			//这里点击获取事件
			if($type=="event" and $event=="CLICK"){
			   $sql="select * from nav where id='".$EventKey."'";
			   $re=mysql_query($sql);
			   
			   $re=mysql_fetch_assoc($re);
			   
			   $content=$re['text'];
			   
			   $xmlTpl = "<xml>
 <ToUserName><![CDATA[%s]]></ToUserName>
 <FromUserName><![CDATA[%s]]></FromUserName>
 <CreateTime>%s</CreateTime>
 <MsgType><![CDATA[text]]></MsgType>
 <Content><![CDATA[%s]]></Content>
 
 </xml>";
             $result = sprintf($xmlTpl, $postObj->FromUserName, $postObj->ToUserName,time(),$content);
			
			 echo $result;
			
			}
			
			
			//新关注欢迎语
			if($type=="event" and $customevent=="subscribe"){
			
				 	$sql="select * from system where id=1";
					
				
					$re=mysql_query($sql);
					
					$tishiyu=mysql_fetch_assoc($re);
			
			
				   $content = $tishiyu['tishiyu'];
				   
				    $xmlTpl = "<xml>
 <ToUserName><![CDATA[%s]]></ToUserName>
 <FromUserName><![CDATA[%s]]></FromUserName>
 <CreateTime>%s</CreateTime>
 <MsgType><![CDATA[text]]></MsgType>
 <Content><![CDATA[%s]]></Content>
 
 </xml>";
             $result = sprintf($xmlTpl, $postObj->FromUserName, $postObj->ToUserName,time(),$content);
			 echo $result;
					
            }
        }
    }

	
      public function https_request($url, $data = null)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if (!empty($data)){
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }
   
	
   
   
    public function xiazai($url){
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_HEADER, 0);    
        curl_setopt($ch, CURLOPT_NOBODY, 0);    //对body进行输出。
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $package = curl_exec($ch);
        $httpinfo = curl_getinfo($ch);
       
        curl_close($ch);
        $media = array_merge(array('mediaBody' => $package), $httpinfo);
        
        //求出文件格式
        preg_match('/\w\/(\w+)/i', $media["content_type"], $extmatches);
        $fileExt = $extmatches[1];
        $filename = time().rand(100,999).".{$fileExt}";
        $dirname = "./images/";
        if(!file_exists($dirname)){
            mkdir($dirname,0777,true);
        }
        file_put_contents($dirname.$filename,$media['mediaBody']);
        return $filename;
    }
	
	
	
public function downloadWeixinFile($url)
{
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_HEADER, 0);    
    curl_setopt($ch, CURLOPT_NOBODY, 0);    //只取body头
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $package = curl_exec($ch);
    $httpinfo = curl_getinfo($ch);
    curl_close($ch);
    $imageAll = array_merge(array('header' => $httpinfo), array('body' => $package)); 
    return $imageAll;
}
 

public function saveWeixinFile($filename, $filecontent)
{
    $local_file = fopen($filename, 'w');
    if (false !== $local_file){
        if (false !== fwrite($local_file, $filecontent)) {
            fclose($local_file);
        }
    }
}

	
	
	
	

	
	
	
	
	/*
------------------------------------------------------
参数：
$str_cut    需要截断的字符串
$length     允许字符串显示的最大长度

程序功能：截取全角和半角（汉字和英文）混合的字符串以避免乱码
------------------------------------------------------
*/
function substr_cut($str_cut,$length)
 {
     if (strlen($str_cut) > $length)
     {
         for($i=0; $i < $length; $i++)
         if (ord($str_cut[$i]) > 128)    $i++;
         $str_cut = substr($str_cut,0,$i)."..";
     }
     return $str_cut;
 }

	private function checkSignature()
	{
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];

		$token = TOKEN;
		$tmpArr = array($token, $timestamp, $nonce);
        // use SORT_STRING rule
		sort($tmpArr,SORT_STRING);
		$tmpStr = implode( $tmpArr );
		$tmpStr = sha1( $tmpStr );

		if( $tmpStr == $signature ){
			return true;
		}else{
			return false;
		}
	}
	
	
	
	public function changesize($mohu){
	
	    	
			include "./phpthumb/ThumbLib.inc.php";
			$img_info = getimagesize($mohu);
		   
		  
            $width=$img_info['0'];
          
			
			if($width<960){
			
				if($width<100){
					  
					  $beishu=1300;
					
					}elseif($width>100 and $width<300){
					
					  $beishu=1000; 
					 
					}elseif($width>300 and $width<600){
					
					  $beishu=400;
					
					}else{
					
					  $beishu=200;
					
					}
				 
				 
				 try {   
					  $thumb = PhpThumbFactory::create($mohu);
					  $thumb->resizePercent($beishu);
					  $thumb->resize(960);
					  $thumb->save($mohu);
					} catch (Exception $e) {   
					print $e->getMessage();   
					exit();   
					}   
			}else{
			
			   $thumb = PhpThumbFactory::create($mohu);
			   $thumb->resize(960);
			   $thumb->save($mohu);
			
			}	
			   
		
	
	
	
	}
	
	
		 //图片二进制编码字符串
  public function pic64($name){
  	       $open    = fopen("./images/".$name,'rb');
           $content = fread($open,filesize("./images/".$name));
           fclose($open);
           $re = base64_encode($content);
           return $re;
  }


  //百度色情图片验证方法
  public function picyanzheng($content,$key){
	  	$ch = curl_init();
	    $url = 'http://apis.baidu.com/idl_baidu/pornfilter/pornfilter';
	    $header = array(
	        'Content-Type:application/x-www-form-urlencoded',
	         'apikey:'.$key.'',
	    );
	    $data = '{
	    "params": [
	        {
	        	"image":"'.$content.'",
	            "versionnum": "1.0",
	            "logid": 1,
	            "cmdid": "cmdid",
	            "appid": "'.$key.'",
	            "type": "type"
	        }
	    ],
	    "jsonrpc": "2.0",
	    "method": "classify",
	    "id": 12345
	   }';
	    // 添加apikey到header
	    curl_setopt($ch, CURLOPT_HTTPHEADER  , $header);
	    // 添加参数
	    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
	    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	    // 执行HTTP请求
	    curl_setopt($ch , CURLOPT_URL , $url);
	    $res = curl_exec($ch);

	    $arr = json_decode($res,true);
	    return $arr['result']['_ret']['confidence_level'];
  }
	
	


}

?>